export enum TimeTypes {
    YYYY_MM_DD = "yyyy-MM-dd",
    YYYY_MM_DD_HAN = "yyyy年MM月dd",
    YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss",
    YYYY_MM_DD_HH_MM_SS_HAN = "yyyy年MM月dd日 HH:mm:ss",
}

/**
 * 根据时间显示问候语
 */
export const timeFix = ()=> {
    const time = new Date()
    const hour = time.getHours()
    return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}
/**
 * 返回多久之前
 * @param time
 */
export const timeAgo = (time: number) => {
    const between = (Date.now() - Number(time))/1000
    if (between < 3600) {
        // tslint:disable-next-line:no-bitwise
        return format(~~(between / 60), ' minute')
    } else if (between < 86400) {
        // tslint:disable-next-line:no-bitwise
        return format(~~(between / 3600), ' hour')
    } else {
        // tslint:disable-next-line:no-bitwise
        return format(~~(between / 86400), ' day')
    }
}

/**
 * 获取当前格式话时间
 * @param type
 */
export const getNowTime = (type: string) => {
    const now = new Date();
    return formatTime(now,type);
}

/**
 * 根据type返回格式化时间
 * @param date
 * @param type
 */
export function formatTime(date: Date, type: string) {

    const year = date.getFullYear();
    const month = date.getMonth();
    const date2 = date.getDate() >= 10 ? date.getDate() : ('0' + date.getDate());
    const hour = date.getHours() >= 10 ? date.getHours() : ('0' + date.getHours());
    const miu = date.getMinutes() >= 10 ? date.getMinutes() : ('0' + date.getMinutes());
    const sec = date.getSeconds() >= 10 ? date.getSeconds() : ('0' + date.getSeconds());
    switch (type) {
        case TimeTypes.YYYY_MM_DD:
            return +year + "-" + (month + 1) + "-" + date2
            break;
        case TimeTypes.YYYY_MM_DD_HAN:
            return +year + "年" + (month + 1) + "月" + date2 + "日 "
            break;
        case TimeTypes.YYYY_MM_DD_HH_MM_SS:
            return +year + "-" + (month + 1) + "-" + date2 + "- " + hour + ":" + miu + ":" + sec;
            break;
        case TimeTypes.YYYY_MM_DD_HH_MM_SS_HAN:
            return +year + "年" + (month + 1) + "月" + date2 + "日 " + hour + ":" + miu + ":" + sec;
            break;
        default:
            return +year + "-" + (month + 1) + "-" + date2 + "- " + hour + ":" + miu + ":" + sec;
            break;

    }

}

/**
 * 字符串时间转时间格式
 * @param dateStr
 */
export const strToDate = (dateStr:string) => {
    return new Date(dateStr);
}

/**
 * 根据开始与结束时间输出该日渐范围所有日期
 * @param startTime yyyy-MM--dd
 * @param endTime  yyyy-MM--dd
 */
export const dateArray = (startTimeStr:string, endTimeStr:string) => {
    const dateList = []
    const startTime = getDate(startTimeStr)
    const endTime = getDate(endTimeStr)
    while ((endTime.getTime() - startTime.getTime()) >= 0) {
        const year = startTime.getFullYear()
        const month = startTime.getMonth() + 1 < 10 ? '0' + (startTime.getMonth() + 1) : startTime.getMonth() + 1
        const day = startTime.getDate().toString().length === 1 ? '0' + startTime.getDate() : startTime.getDate()
        dateList.push(year + '-' + month + '-' + day)
        startTime.setDate(startTime.getDate() + 1)
    }
    return dateList
}

function getDate (datestr) {
    const temp = datestr.split('-')
    const date = new Date(temp[0], temp[1] - 1, temp[2])
    return date
}

/**
 * 格式化时间组合
 * @param time
 * @param label
 */
function format(time: number, label: string) {
    if (time === 1) {
        return time + label
    }
    return time + label + 's'
}

